package com.example.actionsignin.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.actionsignin.R;
import com.example.actionsignin.adapter.ActionAdapter;
import com.example.actionsignin.entity.ActionEntity;
import com.example.actionsignin.entity.ImagesEntity;
import com.example.actionsignin.entity.UserEntity;
import com.example.actionsignin.http.HttpUtil;
import com.example.actionsignin.mvp.view.DescribeActivity;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 旭 on 2017/7/27/027.
 */

public class ActionFragment  extends Fragment implements
        AdapterView.OnItemClickListener{

    private View view;
    private PullToRefreshListView mListView;

    private List<ActionEntity<UserEntity,ImagesEntity>> mList = new ArrayList<>();
    private ActionAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_action,container,false);

        init();

        return view;
    }

    private void init() {
        mListView = (PullToRefreshListView) view.findViewById(R.id.list_action);

        initListView();

        mAdapter = new ActionAdapter(getActivity(),mList);
        mListView.setAdapter(mAdapter);
    }

    //下拉刷新
    private void initListView() {
        ILoadingLayout startLoading = mListView.getLoadingLayoutProxy(true,false);
        startLoading.setPullLabel("正在下拉刷新...");
        startLoading.setRefreshingLabel("正在玩命加载中...");
        startLoading.setReleaseLabel("放开刷新");

        ILoadingLayout endLoading = mListView.getLoadingLayoutProxy(false,true);
        endLoading.setPullLabel("正在上拉刷新...");
        endLoading.setRefreshingLabel("正在玩命加载中...");
        endLoading.setReleaseLabel("放开刷新");

        mListView.setOnItemClickListener(this);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                new RefreshTask().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                new RefreshTask().execute();
            }
        });
    }

    private class RefreshTask extends AsyncTask<Void, Void, String>
    {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Void... params) {

            getData();

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            mListView.onRefreshComplete();
            super.onPostExecute(s);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ActionEntity<UserEntity,ImagesEntity> actionEntity = mList.get(position-1);
        Intent intent = new Intent(getActivity(), DescribeActivity.class);
        intent.putExtra("aid",actionEntity.getAid());

        getActivity().startActivity(intent);
    }

    //获取数据
    private void getData()
    {
        HttpUtil.onSend().getAllAction()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ActionEntity<UserEntity,ImagesEntity>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull List<ActionEntity<UserEntity,ImagesEntity>> list) {
                        mList.clear();
                        for (int i = list.size()-1; i >= 0 ; i--) {
                            ActionEntity<UserEntity,ImagesEntity> action = list.get(i);

                            mList.add(action);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }
}
